using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
public class BuffShowcase : MonoBehaviour
{
    [SerializeField]
    private PlayerController m_TargetEntity;
    [SerializeField]
    private GameObject m_BuffInfoPrefab;

    private ObjectPool<ShowBuffInfo> m_BuffPool;

    [SerializeField]
    private Transform m_PositiveNote;
    [SerializeField]
    private Transform m_NegativeNote;
    [SerializeField]
    private Transform m_PoolNote;

    #region Public方法

    #endregion

    #region Private方法
    private void OnAddBuffListener(BuffBase<PlayerController> buff)
    {
        ShowBuffInfo item = m_BuffPool.Get();
        item.Init(buff, m_BuffPool);
        switch (buff.BuffData.buffType)
        {
            case BuffType.Negative:
                item.transform.SetParent(m_NegativeNote);
                break;
            case BuffType.Positive:
            case BuffType.Neutral:
                item.transform.SetParent(m_PositiveNote);
                break;
        }
        item.transform.SetAsLastSibling();
    }
    #endregion

    #region Unity方法
    private void Awake()
    {
        m_BuffPool = new ObjectPool<ShowBuffInfo>(
            () =>
            {
                return Instantiate(m_BuffInfoPrefab, this.transform).GetComponent<ShowBuffInfo>();
            },
            (item) =>
            {
                item.gameObject.SetActive(true);
            },
            (item) =>
            {
                item.gameObject.SetActive(false);
                item.transform.SetParent(m_PoolNote);
            },
            (item) => { Destroy(item.gameObject); });
    }
    private void Start()
    {
        List<BuffBase<PlayerController>> buffs = m_TargetEntity.BuffSystem.FindAllBuff();
        foreach (BuffBase<PlayerController> buff in buffs)
        {
            OnAddBuffListener(buff);
        }
        m_TargetEntity.BuffSystem.OnAddBuff += OnAddBuffListener;
    }
    private void OnDestroy()
    {
        m_TargetEntity.BuffSystem.OnAddBuff -= OnAddBuffListener;
    }
    #endregion
}
